package com.aier.admin_new.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.common.AndroidType;
import com.aier.admin_new.common.CompresszZipFile;
import com.aier.admin_new.entity.Dynamic;
import com.aier.admin_new.entity.DynamicComment;
import com.aier.admin_new.entity.DynamicMessage;
import com.aier.admin_new.entity.User;
import com.aier.admin_new.service.IChannelClient;
import com.aier.admin_new.service.IDynamicService;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.common.common.SystemConfigStatic;
import com.aier.user.service.IUserService;
import com.baidu.yun.channel.model.BindInfo;

@Scope("prototype")
@Component("DynamicAction")
public class DynamicAction extends AdminNewBaseAction {

	@Resource(name = "dynamicService_new")
	private IDynamicService dynamicService;
	@Resource(name = "channelClient")
	private IChannelClient channelClient;
	@Resource(name = "userService")
	private IUserService userService;

	/**
	 * 动态
	 */
	private static final long serialVersionUID = 1L;

	private int s; // 错误码

	private int page; // 当前页数
	private int page_size; // 每页的记录数
	private int page_count; // 总页数
	private int count; // 总记录数

	private Long uid; // 用户的id
	private Integer sid; // 学校id(学校动态)
	private Integer fuid;// 用于查询好友动态
	private Integer uuid;

	private Long cid; // 班级id(班级动态)
	private String img; // 图片名称
	private String content; // 文字内容
	private Short type; // 动态类型(1学校动态2班级动态3个人动态)

	private Long did; // 动态的id
	private Integer ptype; // 类型：0赞1取消赞
	private Integer pcount; // 赞的次数
	private Integer ccount; // 评论的次数
	private Integer tcount; // 转发的次数
	private Long dcid; // 动态评论的id
	private String rname; // 回复的对象的昵称
	private Long dcrid; // 回复id

	private Dynamic dynamic; // 动态
	private List<Dynamic> dynamicList; // 动态列表
	private List<DynamicComment> dcList; // 动态评论列表
	private List<User> userList; // 动态的赞的用户列表
	private List<DynamicMessage> dmList; // 与我相关的动态消息
	private String photoName; // 上传图片的名称或路径
	private File file;

	@Override
	public String execute() {
		s = 1;
		return SUCCESS;
	}

	public String searchSchoolDynamic() {
		dynamic = dynamicService.searchDynamic(sid);
		s = 1;
		return SUCCESS;
	}

	/**
	 * 删除图片
	 * 
	 * @return
	 */
	public String deleteImage() {
		File fo = new File(SystemConfigStatic.fresh_upload_url + "origin/"
				+ img);
		if (fo.exists()) {
			fo.delete();
		}
		File fs = new File(SystemConfigStatic.fresh_upload_url + "small/" + img);
		if (fs.exists()) {
			fs.delete();
		}
		File fb = new File(SystemConfigStatic.fresh_upload_url + "big/" + img);
		if (fb.exists()) {
			fb.delete();
		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 发布动态
	 * 
	 * @return
	 */
	public String publishDynamic() {
		s = dynamicService.insertDynamic(uid, sid, cid, content, img, type);
		s = 1;
		return SUCCESS;
	}

	/**
	 * 转发动态
	 * 
	 * @return
	 */
	public String transmitDynamic() {
		s = dynamicService
				.insertOtherDynamic(uid, sid, cid, content, did, type);
		Dynamic dy = dynamicService.searchDynamic(did);
		tcount = dy.getTcount();

		com.aier.user.entity.User user = userService.getAppUid(uid);
		List<BindInfo> bf = channelClient.QueryBindList(user.getAppUid());
		if (bf != null) {
			channelClient.PushUnicastNotification("an", bf.get(0)
					.getChannelId(), bf.get(0).getUserId(), "转发动态",
					user.getNickname() + "转发了你的动态",
					AndroidType.TRANSMITCOMMENT, uid);
		}
		return SUCCESS;
	}

	/**
	 * 评论动态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String commentDynamic() throws UnsupportedEncodingException {
		String picName = null;
		if (file != null) {
			picName = CompresszZipFile.uploadDailyfoodImgApp(file, photoName,
					SystemConfigStatic.fresh_upload_url);
		}
		s = dynamicService.insertDynamicComment(uid, did, content, picName);
		ccount = dynamicService.searchDynamic(did).getCcount();

		com.aier.user.entity.User user = userService.getAppUid(uid);
		List<BindInfo> bf = channelClient.QueryBindList(user.getAppUid());
		if (bf != null) {
			channelClient.PushUnicastNotification("an", bf.get(0)
					.getChannelId(), bf.get(0).getUserId(), "评论动态",
					user.getNickname() + "评论了你的动态",
					AndroidType.TRANSMITCOMMENT, uid);
		}
		return SUCCESS;
	}

	/**
	 * 回复动态的评论
	 * 
	 * @return
	 */
	public String replyDynamic() {
		s = dynamicService.insertDCR(uid, dcid, rname, content);

		com.aier.user.entity.User user = userService.getAppUid(uid);
		List<BindInfo> bf = channelClient.QueryBindList(user.getAppUid());
		if (bf != null) {
			channelClient.PushUnicastNotification("an", bf.get(0)
					.getChannelId(), bf.get(0).getUserId(), "回复评论",
					user.getNickname() + "回复了你的评论",
					AndroidType.TRANSMITCOMMENT, uid);
		}
		return SUCCESS;
	}

	/**
	 * 赞或取消赞动态
	 * 
	 * @return
	 */
	public String praiseDynamic() {
		s = dynamicService.updateDynamicPraise(uid, did, ptype);
		Dynamic dy = dynamicService.searchDynamic(did);
		if (dy != null) {
			pcount = dynamicService.searchDynamic(did).getPcount();
		} else {
			pcount = 0;
		}

		if (ptype == 0) {
			com.aier.user.entity.User user = userService.getAppUid(uid);

			List<BindInfo> bf = channelClient.QueryBindList(user.getAppUid());
			if (bf != null) {
				channelClient.PushUnicastNotification("an", bf.get(0)
						.getChannelId(), bf.get(0).getUserId(), "赞评论",
						user.getNickname() + "赞你的评论",
						AndroidType.TRANSMITCOMMENT, uid);
			}
		}
		return SUCCESS;
	}

	/**
	 * 获取学校、班级、个人动态
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchDynamicList() {
		if (page <= 0) {
			page = 1;
		}
		if (page_size <= 0) {
			page_size = Constant.num_10;
		}

		Map<String, Object> map = dynamicService.searchAllDynamic(page,
				page_size, uid, cid, sid, fuid);

		if (null != map) {
			page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
			count = (Integer) map.get(ICommonDAO.COUNT);
			page = (Integer) map.get(ICommonDAO.PAGE);
			dynamicList = (List<Dynamic>) map.get(ICommonDAO.OBJ_LIST);

			if (null != dynamicList && dynamicList.size() > 0) {
				for (Dynamic dynamic : dynamicList) {
					// 转发的动态
					if (dynamic.getOriginal() == 1) {
						dynamic.setDynamic(dynamicService.searchDynamic(dynamic
								.getRelativeid()));
					}

					// 本人是否已赞过（1赞过0没赞）
					dynamic.setPflag((short) dynamicService.searchPraiseBySelf(
							uid, dynamic.getDid()));
					// 获取两条评论
					Map<String, Object> mapC = dynamicService
							.searchCommentByDid(Constant.num_1, Constant.num_2,
									dynamic.getDid());
					dcList = (List<DynamicComment>) mapC
							.get(ICommonDAO.OBJ_LIST);
					dynamic.setDcList(dcList);
				}
			}
		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 获取某条动态的评论
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchCommentByDid() {
		if (page <= 0) {
			page = 1;
		}
		if (page_size <= 0) {
			page_size = Constant.num_10;
		}
		Map<String, Object> map = dynamicService.searchCommentByDid(page,
				page_size, did);
		if (null != map) {
			page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
			count = (Integer) map.get(ICommonDAO.COUNT);
			page = (Integer) map.get(ICommonDAO.PAGE);

			dcList = (List<DynamicComment>) map.get(ICommonDAO.OBJ_LIST);
			if (dcList != null && dcList.size() > 0) {
				for (DynamicComment dc : dcList) {
					dc.setDcrList(dynamicService.searchCRByDcid(dc.getDcid()));
				}
			}

		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 删除动态
	 * 
	 * @return
	 */
	public String deleteDynamic() {
		s = dynamicService.deleteDynamic(did);
		return SUCCESS;
	}

	/**
	 * 删除评论
	 * 
	 * @return
	 */
	public String deleteComment() {
		s = dynamicService.deleteComment(dcid);
		return SUCCESS;
	}

	/**
	 * 删除回复
	 * 
	 * @return
	 */
	public String deleteReply() {
		s = dynamicService.deleteReply(dcrid);
		return SUCCESS;
	}

	/**
	 * 获取某条动态的赞
	 * 
	 * @return
	 */
	public String searchDynamicPraise() {
		userList = dynamicService.searchDynamicPraise(did);
		s = 1;
		return SUCCESS;
	}

	/**
	 * 客户端发布动态
	 * 
	 * @return
	 */
	public String publishDynamicApp() {
		String picName = null;
		if (file != null) {
			picName = CompresszZipFile.uploadDailyfoodImgApp(file, photoName,
					SystemConfigStatic.fresh_upload_url);
		}
		s = dynamicService.insertDynamic(uid, sid, cid, content, picName, type);
		com.aier.user.entity.User u = userService.getAppUid(uid);
		if (type == 1) {
			channelClient.PushTagMessage("school" + sid, "学校动态",
					u.getNickname() + "发布了学校动态#", AndroidType.DYNAMICSCHOOL,
					u.getUid());
		} else if (type == 2) {
			channelClient.PushTagMessage("class" + sid + cid, "班级动态",
					u.getNickname() + " 发布了班级动态#", AndroidType.DYNAMICCLASS,
					u.getUid());
		} else {
			channelClient.PushTagMessage("self" + uid, "个人动态", u.getNickname()
					+ " 发布了个人动态#", AndroidType.DYNAMICPERSON, u.getUid());
		}
		return SUCCESS;
	}

	/**
	 * 获取某条动态的详情
	 * 
	 * @return
	 */
	public String searchDynamicDetail() {
		dynamic = dynamicService.searchDynamicDetail(uid, did);
		s = 1;
		return SUCCESS;
	}

	/**
	 * 获取动态中与我相关的消息（好友评论了我的动态、回复了我的评论、赞了我的动态）
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchDynamicMessage() {
		if (page <= 0) {
			page = 1;
		}
		if (page_size <= 0) {
			page_size = Constant.num_10;
		}
		Map<String, Object> map = dynamicService.searchDynamicMessage(page,
				page_size, uid);
		if (null != map) {
			page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
			count = (Integer) map.get(ICommonDAO.COUNT);
			page = (Integer) map.get(ICommonDAO.PAGE);

			dmList = (List<DynamicMessage>) map.get(ICommonDAO.OBJ_LIST);
		}
		s = 1;
		return SUCCESS;
	}

	public void setDynamicService(IDynamicService dynamicService) {
		this.dynamicService = dynamicService;
	}

	public int getS() {
		return s;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage_size() {
		return page_size;
	}

	public void setPage_size(int page_size) {
		this.page_size = page_size;
	}

	public int getPage_count() {
		return page_count;
	}

	public int getCount() {
		return count;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public void setCid(Long cid) {
		this.cid = cid;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setImg(String img) {
		this.img = img;
	}

	public void setType(Short type) {
		this.type = type;
	}

	public void setDid(Long did) {
		this.did = did;
	}

	public void setPtype(Integer ptype) {
		this.ptype = ptype;
	}

	public Integer getPcount() {
		return pcount;
	}

	public Integer getCcount() {
		return ccount;
	}

	public Integer getTcount() {
		return tcount;
	}

	public List<Dynamic> getDynamicList() {
		return dynamicList;
	}

	public void setPhotoName(String photoName) {
		this.photoName = photoName;
	}

	public void setDcid(Long dcid) {
		this.dcid = dcid;
	}

	public void setRname(String rname) {
		this.rname = rname;
	}

	public List<DynamicComment> getDcList() {
		return dcList;
	}

	public void setDcrid(Long dcrid) {
		this.dcrid = dcrid;
	}

	public List<User> getUserList() {
		return userList;
	}

	public Dynamic getDynamic() {
		return dynamic;
	}

	public List<DynamicMessage> getDmList() {
		return dmList;
	}

	public void setFuid(Integer fuid) {
		this.fuid = fuid;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public void setUuid(Integer uuid) {
		this.uuid = uuid;
	}
	
}
